package system;

/**
 * class Pagination
 * @author Rezan Achmad
 */
public class Pagination {

    // Konstanta CSS
    public static String CSS_SELECTED = "selected";
    public static String CSS_HIDDEN = "hidden";
    public static String CSS_INTERNAL_PAGE = "page";
    public static String CSS_PREVIOUS_PAGE = "prev";
    public static String CSS_NEXT_PAGE = "next";
    
    // Konstanta Label
    public static String LABEL_PREVIOUS = "&lt; Sebelumnya";
    public static String LABEL_NEXT = "Selanjutnya &gt;";
    
    // Konstanta lain-lain
    public static int MAX_BUTTON_COUNT = 10;
    public static String PAGE_PARAMETER = "page";
    
    private String id;
    private String url;
    private int currentPage;
    private int numberPage;

    /**
     * Konstruktor
     * @param url URL 
     * @param currentPage Halaman sekarang
     * @param totalItem Jumlah total item
     */
    public Pagination(String id, String url, int currentPage, int totalItem, int itemPerPage) {
        this.id = id;
        this.url = url;
        this.currentPage = currentPage;
        this.numberPage = (int) Math.round((float) totalItem / (float) itemPerPage);
    }

    /**
     * Get instance
     * @param url URL 
     * @param currentPage Halaman sekarang
     * @param totalItem Jumlah total item
     * @return Objek pagination
     */
    public static Pagination getInstance(String id, String url, int currentPage, int totalItem, int itemPerPage) {
        return new Pagination(id, url, currentPage, totalItem, itemPerPage);
    }

    /**
     * Render Pagination.
     * @return Html pagination.
     */
    public String run() {
        StringBuilder sb = new StringBuilder();
        
        if (numberPage > 1) {
            sb.append("<ul id=\"").append(id).append("\">").append(createPageButtons()).append("</ul>");
        }
        
        return sb.toString();
    }

    /**
     * Membuat semua tombol halaman.
     * @return Html tombol halaman.
     */
    public String createPageButtons() {
        StringBuilder sb = new StringBuilder();

        // Get page range
        int[] pageRange = getPageRange();

        // Create previous page
        sb.append(createPageButton(LABEL_PREVIOUS, ((currentPage > 0) ? currentPage - 1 : currentPage), url, currentPage <= 0, false));

        // Create internal page
        for (int i = pageRange[0]; i <= pageRange[1]; ++i) {
            sb.append(createPageButton(Integer.toString(i + 1), i, CSS_INTERNAL_PAGE, false, currentPage == i));
        }

        // Create next page
        sb.append(createPageButton(LABEL_NEXT, ((currentPage + 1 < numberPage) ? currentPage + 1 : currentPage), url, currentPage + 1 >= numberPage, false));

        return sb.toString();
    }

    /**
     * Mendapat page range.
     * @return Mengembalikan array. Indeks pertama array merupakan beginPage dan
     * index kedua merupakan endPage.
     */
    public int[] getPageRange() {
        // Index pertama : beginPage. Index kedua : endPage.
        int[] pageRange = new int[2];

        pageRange[0] = Math.max(0, currentPage - (int) (MAX_BUTTON_COUNT / 2));
        if ((pageRange[1] = pageRange[0] + MAX_BUTTON_COUNT - 1) >= numberPage) {
            pageRange[1] = numberPage - 1;
            pageRange[0] = Math.max(0, pageRange[1] - MAX_BUTTON_COUNT + 1);
        }

        return pageRange;
    }

    /**
     * Membuat tombol halaman.
     * @param label Label
     * @param page Nomor halaman
     * @param cssClass Atribute css
     * @param hidden Apakah disembunyikan
     * @param selected Apakah ditampilkan
     * @return Mengembalikan html
     */
    public String createPageButton(String label, int page, String cssClass, boolean hidden, boolean selected) {        
        if (selected) cssClass += " " + CSS_SELECTED;
        
        StringBuilder sb = new StringBuilder();
        
        if (!hidden)
            sb.append("<a href=\"").append(createPageUrl(page)).append("\">");
        // li
        sb.append("<li class=\"").append(cssClass).append("\">");
        
        // anchor
        sb.append(label);
        
        sb.append("</li>");
        if (!hidden)
            sb.append("</a>");
        
        return sb.toString();
    }

    /**
     * Membuat url halaman.
     * @param page Nomor halaman.
     * @return Mengembalikan url halaman.
     */
    public String createPageUrl(int page) {
        return url + PAGE_PARAMETER + "=" + page;
    }
}
